package data.join.com.database.bean;

import java.util.List;

import data.join.com.database.GreenDao;

/**
 * Created by Administrator on 2017/10/16.
 */

public class CommandPref {

    public static int HANDLE_STATE_CREATE = 0;
    public static int HANDLE_STATE_HANDLED = 1;
    public static int HANDLE_STATE_ERROR = 2;//error

    private static String TAG;
    private static CommandPref mInstance;

    private CommandBeanDao commandBeanDao;

    private CommandPref(){
        commandBeanDao = GreenDao.getInstance().sDaoSession.getCommandBeanDao();
    }

    public static synchronized CommandPref getInstance() {
        if (mInstance == null) {
            mInstance = new CommandPref();
        }
        return mInstance;
    }

    public void insertCommandBean(CommandBean commandBean){
        commandBeanDao.insert(commandBean);
    }

    public List<CommandBean> queryUnHandleCommand(){
        List<CommandBean> list = null;

        list = commandBeanDao.queryBuilder()
                .where(CommandBeanDao.Properties.HandleState.eq(HANDLE_STATE_CREATE)
                        //,CommandBeanDao.Properties.CreateTime.gt(System.currentTimeMillis()-10*60*1000)
                )
                .list();

        return list;
    }

    public CommandBean getEarliestUnHandleCommand(){
        CommandBean commandBean = null;
        List<CommandBean> list;

        list = commandBeanDao.queryBuilder()
                .where(CommandBeanDao.Properties.HandleState.eq(HANDLE_STATE_CREATE))
                .orderDesc(CommandBeanDao.Properties.Id)
                .limit(1)
                .list();

        if(list != null && list.size() > 0){
            commandBean = list.get(0);
        }

        return commandBean;
    }

    public void updateCommandBean(CommandBean fileUpload){
        commandBeanDao.update(fileUpload);
    }

    public boolean existCommandById(String commandId){
        return commandBeanDao.queryBuilder().where(CommandBeanDao.Properties.CommandId.eq(commandId)).count()>0;
    }
}
